package day27;

import java.awt.BorderLayout;   
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class MapFrame extends JFrame { // MapFrame은 JFrame을 상속한다 JFrame 의 데이터를 끌어다가 MapFrame에서 쓴다

	static HashMap<String,Data> map = new HashMap<String,Data>();  //   --| Data 에서 필요한 설정을 마무리 하고 추가로 적어준다.
	static String fileName= "noname.dat";//                             --|
	
	
	
	
	//문자형의 데이터를 HashMap으로 입력받겠다는 것
	static HashMap<String, Data> map = new HashMap<String, Data>();
	
	private JPanel contentPane; // 고유의 값을 지정해준다식으로 이해....
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmSearch;
	private JMenuItem mntmInput;
	private JMenuItem mntmModify;
	private JMenuItem mntmQuit;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MapFrame frame = new MapFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MapFrame() {
		setTitle("Map Collection Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 522, 375);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		// sample data map.put을 이용해 직접 입력을 한다.
		map.put("a001",new Data("a001","hong","88.01.01", "seoul"));
		map.put("a002",new Data("a002","lee","89.01.01", "busan"));
		map.put("a003",new Data("a003","park","77.01.01", "suwon"));
		map.put("a004",new Data("a004","kim","66.01.01", "incheon"));
		map.put("a005",new Data("a005","choi","55.01.01", "wonju"));
	}
	
	
public void fileSave(){
	FileOutputStream fos = null;
	ObjectOutputStream oos = null;
	try{
		fos = new FileOutputStream(fileName);
		oos = new ObjectOutputStream(fos);
		
		oos.writeObject(map);
		

		oos.close();
		fos.close();
	}catch(Exception ex){
		ex.printStackTrace();
	}
}
	
	
	
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {  
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_1());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("Work");
			mnNewMenu.add(getMntmSearch());
			mnNewMenu.add(getMntmInput());
			mnNewMenu.add(getMntmModify());
			mnNewMenu.add(getMntmQuit());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmSearch() {
		if (mntmSearch == null) {
			mntmSearch = new JMenuItem("Search");
			mntmSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					contentPane.removeAll();
					contentPane.add(new MapSearchPanel());
					contentPane.validate();
				}
			});
		}
		return mntmSearch;
	}
	private JMenuItem getMntmInput() {
		if (mntmInput == null) {
			mntmInput = new JMenuItem("Input");
			mntmInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(new MapInputPanel());
					contentPane.validate();
				}
			});
		}
		return mntmInput;
	}
	private JMenuItem getMntmModify() {
		if (mntmModify == null) {
			mntmModify = new JMenuItem("Modify");
			mntmModify.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(new MapModifyPanel());
					contentPane.validate();

				}
			});
		}
		return mntmModify;
	}
	private JMenuItem getMntmQuit() {
		if (mntmQuit == null) {
			mntmQuit = new JMenuItem("Quit");
			mntmQuit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					MapFrame.this.dispose();
				}
			});
		}
		return mntmQuit;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("New menu");
			mnNewMenu_1.add(getMntmNewMenuItem());
			mnNewMenu_1.add(getMntmNewMenuItem_1());
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("New menu item");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					fileSave();
					
					
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("New menu item");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					
					
				}
			});
		}
		return mntmNewMenuItem_1;
	}
}
